import { queryTaskList,  actionTaskAdd, actionTaskEdit,queryTaskInfo,queryHistoryTaskList, queryDevForTask
} from '../services/task';

export default {
  namespace: 'task',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    loading: {
      reslist: true,
      model_submit:false,
      detailinfo:true,
      curing_submit:false,
      plan_curing:false,
    },
  },

  effects: {
    // 获取列表信息
    *fetch_task_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryTaskList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    *fetch_task_history_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(queryHistoryTaskList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // // 批量删除操作、中止
    // *action_plan_do({ payload }, { call, put }) {
    //   const response = yield call(actionPlanDo, payload);
    // },
    // 导出
    // *action_task_export({ payload }, { call, put }) {
    //   const response = yield call(actionTaskExport, payload);
    // },


    // 新增任务
    *add_task({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionTaskAdd,payload);
      response && (
        yield put({
          type: 'path/fetch_curing_clear',
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 修改任务
    *edit_task({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(actionTaskEdit,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },


    // 获取计划详情
    *fetch_task_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(queryTaskInfo, payload);
      if(response && response.data.taskId){
        // 获取详情塞入右侧设备点
        const devList = yield call(queryDevForTask, { taskId: response.data.taskId});
        devList && devList.data && devList.data.list && (yield put({
          type: 'path/appendPathDev',
          payload: devList.data.list,
        }));
      }
      yield put({
        type: 'appendDataDetail',
        payload:response.data ,
      });
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },


  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },


    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        detaildata:{},
        loading:{}
      };
    },
  },

};
